/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules.NavigationProvider;
using DotNetNuke.UI.WebControls;

using DotNetNuke.Modules.Articles.Common;
using Constants = DotNetNuke.Modules.Articles.Common.Constants;

#endregion

namespace DotNetNuke.Modules.Articles.UI.Controls
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The Mode skin object displays a Mode selector
    /// </summary>
    /// <history>
    /// 	[cnurse]	02/13/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:Mode runat=\"server\"> </{0}:Mode>")]
    public class Mode : ArticleSkinObjectBase
    {
        #region Private Members

        private string _EditCaption;
        private string _EditKey;
        private string _ViewCaption;
        private string _ViewKey;

        #endregion

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Default Css Class
        /// </summary>
        /// <history>
        ///     [cnurse]  03/18/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected override String DefaultCssClass
        {
            get { return "CommandButton"; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the design time text to display
        /// </summary>
        /// <history>
        ///     [cnurse]  04/07/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected override string DesignTimeText
        {
            get { return Constants.TEXT_Mode; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The text of the control in Edit Mode
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/12/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The Edit Mode Caption.")]
        public string EditCaption
        {
            get { return _EditCaption; }
            set { _EditCaption = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A String that contains the localization resource key for the Edit Mode Text
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/12/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The Resource Key to use to localize the Edit Mode Text")]
        public string EditKey
        {
            get { return _EditKey; }
            set { _EditKey = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The text of the control in View Mode
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/12/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The View Mode Caption.")]
        public string ViewCaption
        {
            get { return _ViewCaption; }
            set { _ViewCaption = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// A String that contains the localization resource key for the View Mode Text
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/12/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The Resource Key to use to localize the View Mode Text")]
        public string ViewKey
        {
            get { return _ViewKey; }
            set { _ViewKey = value; }
        }

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderContents renders the contents of the control
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	04/12/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            bool _Show = false;
            bool isEdit = false;
            string buttonText = "Mode";
            string buttonUrl = Null.NullString;
            string image = Constants.IMAGE_Edit;

            if (DesignMode)
                _Show = true;
            else
                _Show = (ParentModule != null) && ParentModule.IsEditable;

            if (_Show)
            {
                if (!DesignMode)
                {
                    int tabId = ParentArticlesModule.TabId;
                    int sectionId = ParentArticlesModule.SectionId;
                    int articleId = ParentArticlesModule.ArticleId;
                    switch (ParentArticlesModule.ViewName)
                    {
                        case Constants.VIEW_EditArticle:
                            isEdit = true;
                            buttonUrl = ParentArticlesModule.ArticleURL(TabId, articleId, Constants.KEY_ViewArticle);
                            break;
                        case Constants.VIEW_EditSection:
                            isEdit = true;
                            buttonUrl = ParentArticlesModule.ArticleURL(TabId, Null.NullInteger, sectionId, Constants.KEY_ViewSection);
                            break;
                        case Constants.VIEW_ViewArticle:
                            buttonUrl = ParentArticlesModule.ArticleURL(tabId, articleId, Constants.KEY_EditArticle);
                            break;
                        case Constants.VIEW_NoArticles:
                        case Constants.VIEW_SectionIntro:
                        case Constants.VIEW_ViewSection:
                            buttonUrl = ParentArticlesModule.ArticleURL(tabId, Null.NullInteger, sectionId, Constants.KEY_EditSection);
                            break;
                        default:
                            break;
                    }

                    if (isEdit)
                    {
                        if (String.IsNullOrEmpty(ViewKey))
                            buttonText = ViewCaption;
                        else
                            buttonText = LocalizeString(ViewKey);
                        image = Constants.IMAGE_View;
                    }
                    else
                    {
                        if (String.IsNullOrEmpty(EditKey))
                            buttonText = EditCaption;
                        else
                            buttonText = LocalizeString(EditKey);
                        image = Constants.IMAGE_Edit;
                    }
                }


                if (!String.IsNullOrEmpty(buttonUrl) || DesignMode)
                {
                    //Render Hyperlink
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, buttonUrl);
                    writer.AddAttribute(HtmlTextWriterAttribute.Title, buttonText);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);

                    //Render Image
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, ResolveClientUrl(image));
                    writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);

                    //Render end of Image
                    writer.RenderEndTag();

                    //Render Hyperlink Text
                    writer.Write(buttonText);

                    //Render end of Hyperlink
                    writer.RenderEndTag();
                }
            }

        }

        #endregion

    }
}
